package Control;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import Entity.MsgPackage;
import Entity.Notification_to_send;
import GUI.ClosureStage_GUI;
import GUI.ScreenManager;

public class ClosuerReq_control {
	private ClosureStage_GUI closure_gui;
	private MsgPackage closure_msg;
	private ArrayList<Integer> result;
	
	public ClosuerReq_control(){
		
		closure_gui = new ClosureStage_GUI();
		closure_msg = new MsgPackage();
		
	}
	/**
	 * this method will get from the data bade all the requests number that needed to be closed
	 * @return ArrayList that conain all the numbers of the requests 
	 */
	public  ArrayList<Integer> getRequestToClose(){
		closure_msg.setOp_code(OP_CODE.GET_CLOSE_REQ);
		closure_msg.setObject(null);
		ScreenManager.getConnection().accept(closure_msg);
			
		Object o = ScreenManager.getConnection().cclient.getMessage();

		 result = (ArrayList<Integer>)o;
		return result;}
	
	public void close_req(Integer req){
		Integer flag;
		
		closure_msg.setOp_code(OP_CODE.CLOSE_REQ);
		closure_msg.setObject(req);
		ScreenManager.getConnection().accept(closure_msg);
			
		Object o = ScreenManager.getConnection().cclient.getMessage();
        flag=(Integer) o;
        if(flag.intValue()==0){
        	JOptionPane.showMessageDialog(null, "The system was unable to close the request", "Error", JOptionPane.ERROR_MESSAGE);		
   
        }
        
    else{
       	closure_msg.setOp_code(OP_CODE.GET_INITIATOR);
		closure_msg.setObject(req);
		ScreenManager.getConnection().accept(closure_msg);
			
		Object w = ScreenManager.getConnection().cclient.getMessage();
    	    
    	    Notification_to_send not=new Notification_to_send();
    	    not.setMessage("request number"+req.toString()+"has been closed");
    	    not.setUser_name((Integer) w);
    	    
			JOptionPane.showMessageDialog(null, "The request was closed successfully", "", JOptionPane.INFORMATION_MESSAGE);		
    }
	
        
		  
	}
		
		
	
	public ClosureStage_GUI getClosure_gui() {
		return closure_gui;
	}
	public void setClosure_gui(ClosureStage_GUI closure_gui) {
		this.closure_gui = closure_gui;
	}
	public MsgPackage getClosure_msg() {
		return closure_msg;
	}
	public void setClosure_msg(MsgPackage closure_msg) {
		this.closure_msg = closure_msg;
	}
	public ArrayList<Integer> getResult() {
		return result;
	}
	public void setResult(ArrayList<Integer> result) {
		this.result = result;
	}

	

}
